var mode;
var oldmatk;
var oldmant;
var row;
var sep = ',';

$(document).ready(function() {
	 
});



function create(){
	mode = "add";
	document.getElementById('create').style.display = 'block';
	document.getElementById('search').style.display = 'none';
	document.getElementById('btnThem').style.display = 'none';

	
	document.getElementById('dknovn').value = 0;
	document.getElementById('dkcovn').value = 0;


	document.getElementById('create_title').innerHTML = "Thêm mới Số dư";
	document.getElementById('createbutton').value = "Thêm";
}    

function isNumberKey(evt){
    var charCode = (evt.which) ? evt.which : event.keyCode
	//alert(charCode);
	if (charCode == 46 )
	{
		return true;
	}
    if (charCode > 31 && (charCode < 48 || charCode > 57))
        return false;
    return true;
}

function validSoTien(nStr, nid)
{
	document.getElementById(nid).value  = addCommas(nStr);

}
function addCommas(nStr, nid)
{    
	if (isNaN(nStr))
	{
		return nStr;
	}
	nStr += '';
    x = nStr.split('.');
    x1 = x[0];
    x2 = x.length > 1 ? '.' + x[1] : '';
	//alert(nStr+"---"+x2);
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x1)) {
        x1 = x1.replace(rgx, '$1' + sep + '$2');
    }
    return x1 + x2;
}



function RTFcreate(){
	document.getElementById('create').style.display = 'none';
	document.getElementById('search').style.display = 'block';
	document.getElementById('btnThem').style.display = 'block';

}    

function edit(pmatk, pmant, pdknovn, pdknont, ida){
	//alert(ida);
	row = ida;
	mode = "edit";
	oldmatk = pmatk;
	oldmant= pmant;

	var sele1=document.getElementById('matk');
	{
		for (var i = 0; i < sele1.options.length; i++) {
			var s = sele1.options[i].text;
			if (s.indexOf(pmatk)==0) {
				sele1.selectedIndex = i;
				break;
			}
		}

	}

	var sele1=document.getElementById('mant');
	{
		for (var i = 0; i < sele1.options.length; i++) {
			var s = sele1.options[i].text;
			if (s.indexOf(pmant)==0) {
				sele1.selectedIndex = i;
				break;
			}
		}

	}

	document.getElementById('dknovn').value = pdknovn;
	document.getElementById('dknont').value = pdknont;

	document.getElementById('createbutton').value = "Ghi";
	document.getElementById('create').style.display = 'block';
	document.getElementById('search').style.display = 'none';
	document.getElementById('btnThem').style.display = 'none';

}    

function SaveNew(host, username, password, databaseName, urlupdate, year, journalid ){
	alert("saviiiiie");
	return;

	matkas = $("#matk option:selected").text();
	matka = matkas.split("-");
	matk =matka[0];

	mant = $("#mant option:selected").text();

	dknovn = document.getElementById('dknovn').value;
	dknont = document.getElementById('dknont').value;
	year = journalid+""+year;

	jQuery.ajax({
       type: "POST",
       url: urlupdate,
	   data: {host: host, username: username, password: password, databaseName: databaseName, matk: matk, mant: mant, dknovn: dknovn, dknont: dknont, year: year, oldmatk: oldmatk, oldmant: oldmant,  mode: mode},
       cache: false,
       success: function(response)
       {
		//alert(response );
		if (response == "2")
		 {
			 alert("Trùng dữ liệu đã có rồi!");
		 }
		 else if (response == "1")
		 {
			 alert("Không có bảng SDTT!");

		 }
		 else if (response == "3")
		 {
			 alert("Co loi du lieu, mau tin khong ghi duoc!");

		 }else
		   {

			if (mode == "add")
			{// refresh list
				document.getElementById('dknovn').value = 0;
				document.getElementById('dknont').value = 0;
				document.getElementById('matk').focus();
				var x=document.getElementById('customers');
				var new_row = x.rows[1].cloneNode(true);
				var tds = new_row.getElementsByTagName('td');
				tds[0].innerHTML = matk;
				tds[1].innerHTML = mant;
				tds[2].innerHTML = dknont;
				tds[3].innerHTML = dknovn;
				tds[4].innerHTML = "";

				x.appendChild( new_row );
			}
			else
		   {
				var x=document.getElementById('customers');
				var new_row = x.rows[row];
				var tds = new_row.getElementsByTagName('td');
				tds[0].innerHTML = matk;
				tds[1].innerHTML = mant;
				tds[2].innerHTML = dknont;
				tds[3].innerHTML = dknovn;

			//RTFcreate();
		   }

		   }
 
       },
	
	   error: function(data, errorThrown)
	  {
		  alert('request failed :'+errorThrown);
	  }
	});

//	if (mode == 'edit')
//	{
//		RTFcreate();
//	}


}

function deledata(host, username, password, databaseName, year, matk, mant, url, urlRFR)
{
	if (!(confirm("Ban muon xoa so du "+ matk+ " "+ mant +"  khong?")))
	{
		return;
	}

	jQuery.ajax({
       type: "POST",
       url: url,
	   data: {host: host, username: username, password: password, databaseName: databaseName, year:year,  matk: matk, mant: mant},
       cache: false,
       success: function(response)
       {
		   var x=document.getElementById('customers');
		   var sodong = x.rows.length ;
			for (i=1;i< sodong ; i++)
			{
				var row = x.rows[i];
				var a1 = row.getElementsByTagName('td');

				if (a1[0].innerHTML == matk && a1[1].innerHTML == mant)
				{
					document.getElementById("customers").deleteRow(i);
					i =i - 1;
					sodong = sodong - 1;
				}
			}

		   //location.href = urlRFR;
       },
	
	   error: function(data, errorThrown)
	  {
		  alert('request failed :'+errorThrown);
	  }
     });
	return;

}
